const router = require('koa-router')()
const { exec } = require('../sql')

// 接口和数据库结合使用
// async  / await 结合promise处理异步
// 让代码变成同步输出

router.get('/api/users', async (ctx, next) => {
  const { username, password } = ctx.query
  try {
    const data = await exec(
      `select * from users where username=? and password=?`,
      [username, password]
    )

    ctx.body = data
  } catch (err) {
    ctx.body = err
  }
})

module.exports = router
